#include "daycheckview.h"
#include <QHeaderView>
#include <QMessageBox>
#include <QDebug>

DayCheckView::DayCheckView(QWidget *parent) : QWidget(parent),
    m_adminManager(nullptr),
    m_attendanceTable(nullptr),
    m_empIdEdit(nullptr),
    m_startDateEdit(nullptr),
    m_endDateEdit(nullptr),
    m_queryButton(nullptr)
{
    initUI();
}

void DayCheckView::initUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(10, 10, 10, 10);
    mainLayout->setSpacing(10);

    // 创建查询条件区域
    QWidget *queryWidget = new QWidget(this);
    QHBoxLayout *queryLayout = new QHBoxLayout(queryWidget);

    // 员工ID输入
    queryLayout->addWidget(new QLabel("员工ID:"));
    m_empIdEdit = new QLineEdit(this);
    m_empIdEdit->setPlaceholderText("可选");
    queryLayout->addWidget(m_empIdEdit);

    // 开始日期
    queryLayout->addWidget(new QLabel("开始日期:"));
    m_startDateEdit = new QDateEdit(QDate::currentDate().addDays(-30), this);
    m_startDateEdit->setCalendarPopup(true);
    queryLayout->addWidget(m_startDateEdit);

    // 结束日期
    queryLayout->addWidget(new QLabel("结束日期:"));
    m_endDateEdit = new QDateEdit(QDate::currentDate(), this);
    m_endDateEdit->setCalendarPopup(true);
    queryLayout->addWidget(m_endDateEdit);

    // 查询按钮
    m_queryButton = new QPushButton("查询", this);
    connect(m_queryButton, &QPushButton::clicked, this, &DayCheckView::onQueryButtonClicked);
    queryLayout->addWidget(m_queryButton);

    mainLayout->addWidget(queryWidget);

    // 创建打卡记录表格
    m_attendanceTable = new QTableWidget(this);
    m_attendanceTable->setColumnCount(7);
    m_attendanceTable->setHorizontalHeaderLabels({"记录ID", "员工ID", "员工姓名", "签到时间", "签退时间", "日期", "状态"});
    m_attendanceTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    mainLayout->addWidget(m_attendanceTable);

    setLayout(mainLayout);
    setWindowTitle("员工打卡详情");
    resize(800, 600);
}

void DayCheckView::setAdminManager(AdminManager *manager)
{
    m_adminManager = manager;
    if (m_adminManager)
    {
        // 连接操作结果信号
        connect(m_adminManager, &AdminManager::operationResult, this, &DayCheckView::onOperationResult);
        // 初始加载数据
        refreshAttendanceData();
    }
}

void DayCheckView::refreshAttendanceData()
{
    if (!m_adminManager)
    {qDebug()<<222;
        QMessageBox::warning(this, "警告", "未设置管理员管理器");
        return;
    }

    // 获取查询条件
    QString empId = m_empIdEdit->text().trimmed();
    QString startDate = m_startDateEdit->date().toString("yyyy-MM-dd");
    QString endDate = m_endDateEdit->date().toString("yyyy-MM-dd");

    // 查询打卡数据
    QList<QVariantList> attendanceData = m_adminManager->getEmployeeAttendance(empId, startDate, endDate);

    // 清空表格
    m_attendanceTable->setRowCount(0);

    // 填充表格
    for (const QVariantList &rowData : attendanceData)
    {
        int row = m_attendanceTable->rowCount();
        m_attendanceTable->insertRow(row);

        for (int col = 0; col < rowData.size() && col < m_attendanceTable->columnCount(); ++col)
        {
            m_attendanceTable->setItem(row, col, new QTableWidgetItem(rowData[col].toString()));
        }
    }
}

void DayCheckView::onQueryButtonClicked()
{
    refreshAttendanceData();
}

void DayCheckView::onCheckInButtonClicked()
{
    if (!m_adminManager)
    {
        QMessageBox::warning(this, "警告", "未设置管理员管理器");
        return;
    }

    // 获取当前选中的员工ID
    QString empId = m_empIdEdit->text().trimmed();
    if (empId.isEmpty())
    {
        QMessageBox::warning(this, "警告", "请输入员工ID");
        return;
    }

    // 执行签到操作
    // 注意：这里假设AdminManager有checkIn方法，实际可能需要根据项目结构调整
    // bool success = m_adminManager->checkIn(empId);
    // 由于没有checkIn方法，这里先做占位处理
    QMessageBox::information(this, "提示", "签到功能尚未实现");
}

void DayCheckView::onCheckOutButtonClicked()
{
    if (!m_adminManager)
    {
        QMessageBox::warning(this, "警告", "未设置管理员管理器");
        return;
    }

    // 获取当前选中的员工ID
    QString empId = m_empIdEdit->text().trimmed();
    if (empId.isEmpty())
    {
        QMessageBox::warning(this, "警告", "请输入员工ID");
        return;
    }

    // 执行签退操作
    // 注意：这里假设AdminManager有checkOut方法，实际可能需要根据项目结构调整
    // bool success = m_adminManager->checkOut(empId);
    // 由于没有checkOut方法，这里先做占位处理
    QMessageBox::information(this, "提示", "签退功能尚未实现");
}

void DayCheckView::onOperationResult(bool success, const QString &message)
{
    if (success)
    {
        QMessageBox::information(this, "成功", message);
        // 如果操作成功，刷新打卡数据
        refreshAttendanceData();
    }
    else
    {
        QMessageBox::critical(this, "失败", message);
    }
}

void DayCheckView::updateAttendanceStatus()
{
    // 更新打卡状态
    // 这里可以根据业务逻辑更新UI显示
    // 例如，根据当前时间和打卡记录显示签到/签退状态
    refreshAttendanceData();
}
